﻿using SuperfastBlur;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model {
    class Captcha {
        public static string Text { get; private set; }

        /// <summary>
        /// 生成图片验证码，返回Bitmap对象
        /// </summary>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <param name="codeLength">验证码长度</param>
        public static Bitmap getImage(int width, int height, int codeLength) {
            Bitmap bmp = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(bmp);
            Random random = new Random();

            //字体组/浅色/深色
            string[] fontFamily = { "Segoe UI", "等线", "黑体", "隶书", "方正姚体", "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Color[] lightColor = { Color.AliceBlue, Color.AntiqueWhite, Color.Aquamarine, Color.Azure, Color.Beige, Color.Bisque, Color.BlanchedAlmond, Color.Cornsilk, Color.FloralWhite, Color.Gainsboro, Color.GhostWhite, Color.LightBlue, Color.LightGoldenrodYellow, Color.LightGreen, Color.LightGray, Color.LightPink, Color.LightSteelBlue, Color.LightYellow, Color.LightSkyBlue, Color.White, Color.WhiteSmoke };
            Color[] darkColor = { Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, Color.DarkGreen, Color.DarkOliveGreen, Color.DarkOrange, Color.DarkOrchid, Color.DarkRed, Color.DarkSalmon, Color.DarkSlateBlue, Color.DarkSlateGray, Color.DarkViolet };
            Text = string.Empty;

            //抗锯齿/初始化画布
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            graphics.Clear(lightColor[random.Next(lightColor.Length)]);

            //字符占位
            int fontCell = (int)(Math.Min(height, width / codeLength) * .8);

            int limit = (int)Math.Sqrt(width * height);
            //添加随机噪点
            for (int i = 0; i < limit * 10; i++) {
                bmp.SetPixel(
                    random.Next(width),
                    random.Next(height),
                    darkColor[random.Next(darkColor.Length)]
                );
            }
            //添加随机干扰线
            for (int i = 0; i < limit; i++) {
                graphics.DrawLine(
                    new Pen(lightColor[random.Next(lightColor.Length)]),
                    random.Next(width), random.Next(height), random.Next(width), random.Next(height)
                );
            }


            //随机字符
            for (int i = 0; i < codeLength; i++) {
                //除去数字0，避免0和O分不清
                int num = 1 + random.Next(35);

                //数字转1-9,A-Z
                char C = num > 9 ? (char)('A' + num - 10) : (char)('0' + num);

                //记录字符
                Text += C;

                //位置偏移值
                int randomOffset = (int)(fontCell * .2);
                //上方空间
                int paddingTop = (int)((height - fontCell) * .4) - randomOffset;
                //移动光标
                int dx = i * fontCell + random.Next(randomOffset), dy = paddingTop + random.Next(randomOffset);
                graphics.TranslateTransform(dx, dy);
                //旋转光标
                float angle = random.Next(-30, 30);
                graphics.RotateTransform(angle);


                //绘制字符
                graphics.DrawString(
                    C.ToString(),
                    new Font(fontFamily[random.Next(fontFamily.Length)], (int)((fontCell + random.Next(randomOffset)) * .9), FontStyle.Bold),
                    new SolidBrush(darkColor[random.Next(darkColor.Length)]), 0, 0
                );

                //还原光标
                graphics.RotateTransform(-angle);
                graphics.TranslateTransform(-dx, -dy);
            }
            return bmp;
        }
    }
}
